package com.neuedu.action.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.model.Food;
import com.neuedu.model.FoodMake;
import com.neuedu.model.FoodType;
import com.neuedu.model.Info;
import com.neuedu.model.Notice;
import com.neuedu.service.FoodMakeService;
import com.neuedu.service.FoodService;
import com.neuedu.service.FoodTypeService;
import com.neuedu.service.InfoService;
import com.neuedu.service.NoticeService;

@Controller
@RequestMapping(value = "/IndexWeb")
public class IndexWebAction {
	@Autowired
	private NoticeService noticeService;

	@Autowired
	private FoodService foodService;

	@Autowired
	private InfoService infoService;

	@Autowired
	private FoodTypeService foodTypeService;

	@Autowired
	private FoodMakeService foodMakeService;

	// 只查询标题
	@RequestMapping(value = "/findIndex")
	public String findIndex(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("===========================================================");

		// 公告栏
		List<Notice> c = noticeService.findTitle();
		request.setAttribute("titleList", c);

		// 推荐新菜谱
		List<Food> n = foodService.findNewest();
		request.setAttribute("newest", n);
		
		

		// 前台查询5条最新菜谱
		List<Food> f = foodService.findTop();
		request.setAttribute("foodList", f);

		// 美食类型
		List<FoodType> ft = foodTypeService.findType();
		request.setAttribute("foodtype", ft);

		// 美食做法
		List<FoodMake> fm = foodMakeService.findMake();
		request.setAttribute("foodmake", fm);

		return "index";

	}

	// 公告详情
	@RequestMapping(value = "/findNoticeById")
	public String findNoticeById(Notice notice, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 所有公告
		Notice n = noticeService.findNoticeById(notice);

		if (n != null) {
			System.out.println("=====查到有公告======");
		}
		request.setAttribute("noticedetail", n);

		// 公告栏
		List<Notice> c = noticeService.findTitle();
		request.setAttribute("titleList", c);

		// 美食类型
		List<FoodType> ft = foodTypeService.findType();
		request.setAttribute("foodtype", ft);

		// 美食做法
		List<FoodMake> fm = foodMakeService.findMake();
		request.setAttribute("foodmake", fm);

		return "noticedetail";

	}

	// 查找所有资讯
	@RequestMapping(value = "/findAllInfo")
	public String findAllInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 所有资讯
		List<Info> i = infoService.findAllInfo();
		request.setAttribute("allinfo", i);

		// 美食类型
		List<FoodType> ft = foodTypeService.findType();
		request.setAttribute("foodtype", ft);

		// 美食做法
		List<FoodMake> fm = foodMakeService.findMake();
		request.setAttribute("foodmake", fm);

		return "info";
	}

	// 资讯详情
	@RequestMapping(value = "/findInfoById")
	public String findInfoById(Info info, HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 所有资讯
		Info i = infoService.findInfoById(info);
		request.setAttribute("infodetail", i);

		// 美食做法
		List<FoodMake> fm = foodMakeService.findMake();
		request.setAttribute("foodmake", fm);

		// 美食类型
		List<FoodType> ftp = foodTypeService.findType();
		request.setAttribute("foodtype", ftp);

		return "infodetail";

	}

}
